package com.prs.crm.action.ca.work;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.ca.work.ArticleInfo;
import com.prs.crm.domain.ca.work.ArticleTypes;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.approval.ShenQingTypeService;
import com.prs.crm.service.approval.impl.ShenQingTypeServiceImpl;
import com.prs.crm.service.ca.work.ArticleInfoService;
import com.prs.crm.service.ca.work.ArticleTypesService;
/**
 * @author YangHao
 * @E-Mail:450098606@qq.com
 * @qq:450098606
 * @version 创建时间：2012-4-16 10:32:20 物品信息
 */
public class ArticleInfoAction extends BaseAction{

	private static final long serialVersionUID = 8159874806987380670L;
	private ArticleInfo articleInfo;
	private ArticleInfoService articleInfoService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Collection<ArticleTypes> articleTypes;
	private ArticleTypesService articleTypesService;
	private Application application;
	private ApplicationService ApplicationService;
	private ShenQingTypeService shenQingTypeService = new ShenQingTypeServiceImpl();
	
	public Application getApplication() {
		return application;
	}
	public void setApplication(Application application) {
		this.application = application;
	}
	public ApplicationService getApplicationService() {
		return ApplicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}
	public ShenQingTypeService getShenQingTypeService() {
		return shenQingTypeService;
	}
	public void setShenQingTypeService(ShenQingTypeService shenQingTypeService) {
		this.shenQingTypeService = shenQingTypeService;
	}
	public ArticleInfo getArticleInfo() {
		return articleInfo;
	}
	public void setArticleInfo(ArticleInfo articleInfo) {
		this.articleInfo = articleInfo;
	}

	public Collection<ArticleTypes> getArticleTypes() {
		return articleTypes;
	}
	public void setArticleTypes(Collection<ArticleTypes> articleTypes) {
		this.articleTypes = articleTypes;
	}
	
	public ArticleTypesService getArticleTypesService() {
		return articleTypesService;
	}
	public void setArticleTypesService(ArticleTypesService articleTypesService) {
		this.articleTypesService = articleTypesService;
	}
	public ArticleInfoService getArticleInfoService() {
		return articleInfoService;
	}
	public void setArticleInfoService(ArticleInfoService articleInfoService) {
		this.articleInfoService = articleInfoService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	@Action("listArticleInfo")
	public String listArticleInfo(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ArticleInfo.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getArticleInfoService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("selectArticleInfo")
	public String selectArticleInfo(){
		this.listArticleInfo();
		return SUCCESS;
	}
	@Action(value = "removeArticleInfo", results = {
			@Result(name = "success", type = "redirect", location = "listArticleInfo"),
			@Result(name = "error", type = "chain", location = "listArticleInfo") })
	public String removeArticleInfo(){
		if (ids==null) {
			this.addActionError("请选择操作行！");
			return ERROR;
		}
		this.getArticleInfoService().batchRemove(ids);
		return SUCCESS;
	}
	
	
	@Action("editArticleInfo")
	public String editArticleInfo(){
		this.articleTypes=this.getArticleTypesService().getAllArticleTypes();
		return SUCCESS;
	}
	
	@Action(value = "saveArticleInfo", results = {
			@Result(name = "success", type = "redirect", location = "listArticleInfo"),
			@Result(name = "error", type = "chain", location = "editArticleInfo") })
	public String saveArticleInfo(){
		if (articleInfo.getAi_articleType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}else if ("".equals(articleInfo.getAi_articleName())) {
			this.addActionError("品名不能为空！");
			return ERROR;
		}else if("".equals(articleInfo.getAi_articleUnit())){
			this.addActionError("单位不能为空！");
			return ERROR;
		}else if("".equals(articleInfo.getAi_articleUnivalence())){
			this.addActionError("单价不能为空！");
			return ERROR;
		}else{
			this.getArticleInfoService().save(articleInfo);
			application = new Application();
			application.setType("一般");
			application.setAppName("[一般]"+"物品信息");
			application.setSqType(2);
			application.setSpr(0);
			application.setPerson(articleInfo.getRecorder());
			application.setArticleInfo(articleInfo);
			application.setResign("物品信息");
			//application.setShenQingType(this.getShenQingTypeService().getShenQingType("物品信息"));
			this.getApplicationService().save(application);
			return SUCCESS;
		}
		
	}
}
